package io.renren.modules.avanotes.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.vo.output.SpecialSubjectVo;
import io.renren.modules.avanotes.entity.SpecialSubjectEntity;
import io.renren.modules.avanotes.service.SpecialSubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 专题类目表
 *
 * @author zzr
 * @email 529742454@qq.com
 * @date 2022-02-19 15:17:28
 */
@RestController
@RequestMapping("avanotes/specialsubject")
@Api("专题类目表接口")
public class SpecialSubjectController {
    @Autowired
    private SpecialSubjectService specialSubjectService;

    //region 自定义业务

    /**
     * 获取所有数据
     *
     * @return
     */
    @PostMapping("/list/tree/{mindBookId}")
    @ApiOperation("查询列表")
    public R listTree(@PathVariable("mindBookId") Integer mindBookId) {
        List<SpecialSubjectVo> list = specialSubjectService.listTree(mindBookId);

        return R.ok().setData(list);
    }

    @PostMapping("/queryPageByScheduleId")
    @ApiOperation("查询列表")
    public R queryPageByScheduleId(@ApiIgnore @RequestParam Map<String, Object> params) {
        IPage<SpecialSubjectEntity> simplePage = Query.getSimplePage(params);
        Object sId = params.get("scheduleId");
        PageUtils page;
        if (sId == null || StringUtils.isBlank(sId.toString())) {
            page = new PageUtils(simplePage);
        } else {
            page = specialSubjectService.queryPageByScheduleId(simplePage, Long.parseLong(params.get("scheduleId").toString()));
        }

        return R.ok().setData(page);
    }

    //endregion


    /**
     * 列表
     */
    @PostMapping("/list")
    //@RequiresPermissions("avanotes:specialsubject:list")
    //@ApiImplicitParams({
    //        @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "String"),
    //        @ApiImplicitParam(name = "limit", value = "每页记录数", paramType = "query", dataType = "String"),
    //        @ApiImplicitParam(name = "key", value = "检索关键字", paramType = "query", dataType = "String")
    //})
    //@ApiOperation("查询列表")
    public R list(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageUtils page = specialSubjectService.queryPage(params);

        //return R.ok().put("page", page);
        return R.ok().setData(page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("avanotes:specialsubject:info")
    //@ApiOperation("查询单条")
    public R info(@PathVariable("id") Integer id) {
        SpecialSubjectEntity specialSubject = specialSubjectService.getById(id);

        //return R.ok().put("specialSubject", specialSubject);
        return R.ok().setData(specialSubject);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("avanotes:specialsubject:save")
    //@ApiOperation("保存")
    public R save(@RequestBody SpecialSubjectEntity specialSubject) {
        specialSubjectService.save(specialSubject);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("avanotes:specialsubject:update")
    //@ApiOperation("修改")
    public R update(@RequestBody SpecialSubjectEntity specialSubject) {
        specialSubjectService.updateById(specialSubject);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("avanotes:specialsubject:delete")
    //@ApiOperation("删除")
    public R delete(@RequestBody Integer[] ids) {
        specialSubjectService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
